package com.edu.bhu.admin.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.edu.bhu.admin.domain.HouHouseAndTag;
import com.edu.bhu.admin.domain.HouImages;
import com.edu.bhu.admin.domain.HouTag;
import com.edu.bhu.admin.mapper.HouHouseAndTagMapper;
import com.edu.bhu.admin.mapper.HouImagesMapper;
import com.edu.bhu.admin.mapper.HouTagMapper;
import com.edu.bhu.common.utils.DateUtils;
import com.edu.bhu.common.utils.StringUtils;
import com.edu.bhu.system.domain.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.bhu.admin.mapper.HouHouseMapper;
import com.edu.bhu.admin.domain.HouHouse;
import com.edu.bhu.admin.service.IHouHouseService;
import com.edu.bhu.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import static com.edu.bhu.common.utils.ShiroUtils.getSysUser;

/**
 * 房屋Service业务层处理
 * 
 * @author zhangkeyi
 * @date 2022-02-22
 */
@Service
public class HouHouseServiceImpl implements IHouHouseService 
{
    @Autowired
    private HouHouseMapper houHouseMapper;

    @Autowired
    private HouHouseAndTagMapper houHouseAndTagMapper;

    @Autowired
    private HouTagMapper houTagMapper;

    @Autowired
    private HouImagesMapper houImagesMapper;

    /**
     * 查询房屋
     * 
     * @param houseId 房屋主键
     * @return 房屋
     */
    @Override
    public HouHouse selectHouHouseByHouseId(Long houseId)
    {
        return houHouseMapper.selectHouHouseByHouseId(houseId);
    }

    /**
     * 查询房屋（关联明细）
     *
     * @param houseId 房屋主键
     * @return 房屋
     */
    public HouHouse selectHouHouseByHouseIdWithDetail(Long houseId)
    {
        return houHouseMapper.selectHouHouseByHouseIdWithDetail(houseId);
    }

    /**
     * 查询房屋列表
     * 
     * @param houHouse 房屋
     * @return 房屋
     */
    @Override
    public List<HouHouse> selectHouHouseList(HouHouse houHouse)
    {
        List<HouHouse> list = houHouseMapper.selectHouHouseList(houHouse);
        if("1".equals(houHouse.getParams().get("listAll")))
            list.stream().map(item -> {
                List<HouTag> tags = houTagMapper.selectHouTagByHouseId(item.getHouseId());
                List<HouImages> images = houImagesMapper.selectHouImagesListByHouseId(item.getHouseId());
                //获取房屋对应的所有标签
                item.setHouTags(tags);
                //获取图片列表中的第一张
                if(images.size()!=0)
                    item.setHouImage(images.get(0));
                return item;
            }).collect(Collectors.toList());
        return list;
    }

    /**
     * 查询房屋列表（关联）
     *
     * @param houHouse 房屋
     * @return 房屋
     */
    @Override
    public List<HouHouse> selectHouHouseListWithDetail(HouHouse houHouse)
    {
        return houHouseMapper.selectHouHouseListWithDetail(houHouse);
    }

    /**
     * 新增房屋
     * 
     * @param houHouse 房屋
     * @return 结果
     */
    @Override
    public int insertHouHouse(HouHouse houHouse)
    {
        houHouse.setUserId(getSysUser().getUserId());
        houHouse.setUpdateDate(DateUtils.getNowDate());
        return houHouseMapper.insertHouHouse(houHouse);
    }

    /**
     * 修改房屋
     * 
     * @param houHouse 房屋
     * @return 结果
     */
    @Override
    public int updateHouHouse(HouHouse houHouse)
    {
        houHouse.setUpdateDate(DateUtils.getNowDate());
        return houHouseMapper.updateHouHouse(houHouse);
    }

    /**
     * 房屋状态修改
     *
     * @param houHouse 房屋信息
     * @return 结果
     */
    @Override
    public int changeStatus(HouHouse houHouse)
    {
        houHouse.setUpdateDate(DateUtils.getNowDate());
        return houHouseMapper.updateHouHouse(houHouse);
    }

    /**
     * 批量删除房屋
     * 
     * @param houseIds 需要删除的房屋主键
     * @return 结果
     */
    @Override
    public int deleteHouHouseByHouseIds(String houseIds)
    {
        return houHouseMapper.deleteHouHouseByHouseIds(Convert.toStrArray(houseIds));
    }

    /**
     * 删除房屋信息
     * 
     * @param houseId 房屋主键
     * @return 结果
     */
    @Override
    public int deleteHouHouseByHouseId(Long houseId)
    {
        return houHouseMapper.deleteHouHouseByHouseId(houseId);
    }

    /**
     * 房屋授权标签
     *
     * @param houseId
     * @param tagIds
     */
    @Override
    @Transactional
    public void insertHouseAuth(Long houseId, Long[] tagIds)
    {
        houHouseAndTagMapper.deleteHouHouseAndTagByHouseId(houseId);
        insertHouseTag(houseId, tagIds);
    }

    /**
     * 新增房屋标签信息
     *
     * @param houseId
     * @param tagIds
     */
    public void insertHouseTag(Long houseId, Long[] tagIds)
    {
        if (StringUtils.isNotNull(tagIds))
        {
            // 新增房屋与标签管理
            List<HouHouseAndTag> list = new ArrayList<>();
            for (Long tagId : tagIds)
            {
                HouHouseAndTag ht = new HouHouseAndTag();
                ht.setHouseId(houseId);
                ht.setTagId(tagId);
                list.add(ht);
            }
            if (list.size() > 0)
            {
                houHouseAndTagMapper.batchHouseTag(list);
            }
        }
    }


}
